package com.base.cn.platform.os.controller.user.address;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.user.address.CusUserAddress;
import com.base.cn.platform.os.service.user.address.CusUserAddressBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 用户收货地址
 * @author ll
 * @date 2018/5/28 11:09
 */
@RestController
@RequestMapping("/manage/user")
public class CusUserAddressController extends BaseController {
    @Autowired
    private CusUserAddressBiz cusUserAddressBiz;

    /**
     * 保存修改用户
     * @param userAddress 用户地址
     * @return
     */
    @RequestMapping("/saveUpdateCusUserAddress")
    public Map<String, Object> saveUpdateCusUserAddress(@RequestBody CusUserAddress userAddress){
        if (!DataUtil.idIsNotNull(userAddress.getUserId())) {
            return ResultUtil.ERROR("请登录再操作");
        }
        if (StringUtils.isEmpty(userAddress.getReceiver())) {
            return ResultUtil.ERROR("请填写收货人名","userNameError");
        }
      /*  if (!DataUtil.idIsNotNull(userAddress.getProvinceId())
                || !DataUtil.idIsNotNull(userAddress.getCityId())
                || !DataUtil.idIsNotNull(userAddress.getTownId())) {
            return ResultUtil.ERROR("请选择地区","addressError");
        }*/
        if (StringUtils.isEmpty(userAddress.getAddress())) {
            return ResultUtil.ERROR("请填写街道或楼层单元不能为空","streetError");
        }
        if(!StringUtils.isContainChinese(userAddress.getAddress())){
            return ResultUtil.ERROR("具体到路名/门牌号必须包含有中文","streetError");
        }
        if (StringUtils.isEmpty(userAddress.getMobile())) {
            return ResultUtil.ERROR("请填写收货人手机号","mobileError");
        }
        if (!StringUtils.isMobile(userAddress.getMobile())) {
            return ResultUtil.ERROR("请填写正确的收货人手机号","mobileError");
        }
        if(!DataUtil.idIsNotNull(userAddress.getId())){
            List<CusUserAddress> userAddressList = cusUserAddressBiz.queryUserAddressListByUserId(userAddress.getUserId(),false);
            if(ObjectUtils.isNotEmpty(userAddressList) && userAddressList.size() >= 3){
                return ResultUtil.ERROR("每个用户最多可以添加三条收货地址");
            }
            //如过没有记录则第一条设置为默认地址
            if(ObjectUtils.isEmpty(userAddressList)  ){
                userAddress.setStatus(1);
            }
        }
        CusUserAddress cusUserAddress = cusUserAddressBiz.saveUserAddress(userAddress);
        return ResultUtil.SUCCESS("收货地址保存成功",cusUserAddress);
    }

    /**
     * 搜索地址列表
     * @return
     */
    @RequestMapping("/queryUserAddressListPage")
    public PageInfo<CusUserAddress> queryUserAddressListPage(@RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage,
                                                             @RequestBody CusUserAddress userAddress,
                                                             @RequestParam(value = "setUserAddress",required = false,defaultValue = "false")boolean setUserAddress){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        return cusUserAddressBiz.queryUserAddressListPage(page, userAddress, setUserAddress);
    }

    /**
     * 搜索地址列表
     * @return
     */
    @RequestMapping("/queryUserAddressList")
    public List<CusUserAddress>  queryUserAddressList(@RequestBody CusUserAddress userAddress,
                                                      @RequestParam(value = "setUserAddress",required = false,defaultValue = "false")boolean setUserAddress){
        return cusUserAddressBiz.queryUserAddressList(userAddress,setUserAddress);
    }

    /**
     * 用户Id搜索地址
     * @param userId
     * @param setUserAddress
     * @return
     */
    @RequestMapping("/queryUserAddressListByUserId")
    public List<CusUserAddress> queryUserAddressListByUserId(@RequestParam("userId")BigDecimal userId,
                                                             @RequestParam(value = "setUserAddress",required = false,defaultValue = "false")boolean setUserAddress){
        return cusUserAddressBiz.queryUserAddressListByUserId(userId,setUserAddress);
    }

    /**
     * 修改设置默认地址
     * @param addressId 地址ID
     * @param isFirst 默认状态，1是，2不是
     * @return
     */
    @RequestMapping("/updateUserAddressIsFirst")
    public Map<String,Object> updateUserAddressIsFirst(@RequestParam("addressId")BigDecimal addressId,
                                                       @RequestParam("isFirst")Integer isFirst){
        cusUserAddressBiz.updateUserAddressIsFirst(addressId,isFirst);
        return ResultUtil.SUCCESS("设置默认地址成功");
    }

    /**
     * 根据ID获取地址
     * @param addressId
     * @return
     */
    @RequestMapping("/queryUserAddressById")
    public CusUserAddress queryUserAddressById(@RequestParam("addressId")BigDecimal addressId,
                                               @RequestParam(value = "setUserAddress",required = false,defaultValue = "false")boolean setUserAddress){
        return cusUserAddressBiz.queryUserAddressById(addressId, setUserAddress);
    }

    /**
     * 根据ID删除
     * @param addressId
     * @return
     */
    @RequestMapping("/deleteUserAddressById")
    public Map<String,Object> deleteUserAddressById(@RequestParam("addressId")BigDecimal addressId){
        cusUserAddressBiz.deleteUserAddressById(addressId);
        return ResultUtil.SUCCESS("删除成功");
    }

}
